<template>
  <el-dialog :title="title" :visible.sync="isOpen" append-to-body @close="handleClose" width="75%">
    <div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="站办所" prop="postDict">
          <el-cascader
            v-model="queryParams.deptId"
            :options="deptOptions"
            :props="{ checkStrictly: true, value: 'deptId', label: 'deptName', checkStrictly: true, emitPath: false }"
            placeholder="选择所属站办所"
            @change="handleDeptChange"
            ref="CascaderRef"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="queryParams.name" placeholder="输入姓名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="queryParams.phone" placeholder="输入手机号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="类型" prop="personnelTypeDict">
          <el-select v-model="queryParams.personnelTypeDict" placeholder="选择类型" clearable @change="handleQuery">
            <el-option v-for="dict in dict.type.biz_personnel_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="tableList"
        :highlight-current-row="!multiple"
        @current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
        ref="table"
      >
        <el-table-column v-if="multiple" type="selection" width="50" align="center" />
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门" align="center" min-width="120" key="deptName" prop="deptName" :show-overflow-tooltip="true" />
        <el-table-column label="姓名" align="center" min-width="120" key="name" prop="name" :show-overflow-tooltip="true"> </el-table-column>
        <!-- <el-table-column label="性别" align="center" key="sexDict" prop="sexDict" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sexDict" />
          </template>
        </el-table-column> -->
        <el-table-column label="类型" align="center" min-width="100" key="personnelTypeDict" prop="personnelTypeDict" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.biz_personnel_type" :value="scope.row.personnelTypeDict" />
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center" min-width="120" key="phone" prop="phone" :show-overflow-tooltip="true" />
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { list } from '@/api/organization/civil-servant'
import { deptTreeSelect } from '@/api/system/user'

export default {
  name: 'dept_check-civil-servant',
  dicts: ['biz_personnel_type', 'sys_user_sex', 'biz_civil_servant_post', 'biz_civil_servant_special_situation'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  components: {},
  data() {
    return {
      // 部门树选项
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        personnelTypeDict: undefined,
        postDict: undefined,
        deptId: ''
      },
      selections: '',
      title: '站办所成员',
      ids: '',
      isOpen: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      tableList: [],
      loading: false
    }
  },
  computed: {},
  watch: {
    visible: {
      handler(val) {
        this.isOpen = val
        if (val) {
          this.getDeptTree()
        }
      }
    }
  },
  methods: {
    // 选择站办所
    handleDeptChange() {
      this.handleQuery()
      this.$refs.CascaderRef.dropDownVisible = false
    },
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
        this.queryParams.deptId = this.deptOptions[0].deptId
        this.getList()
      })
    },
    handleConfirm() {
      this.$emit('success', this.selections)
      this.handleClose()
    },
    getList() {
      this.loading = true
      list(this.queryParams).then(response => {
        this.tableList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 单选选中数据
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selections = currentRow
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selections = selection
      this.ids = selection.map(item => item.id)
    },
    // 关闭弹框
    handleClose() {
      this.resetForm('queryForm')
      this.$refs.table.clearSort()
      if (this.multiple) {
        this.$refs.table.clearSelection()
      } else {
        this.$refs.table.setCurrentRow()
      }
      this.$emit('update:visible', false)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.$refs.table.clearSort()
      if (this.multiple) {
        this.$refs.table.clearSelection()
      } else {
        this.$refs.table.setCurrentRow()
      }
      this.getDeptTree()
    }
  }
}
</script>

<style lang="scss"></style>
